package cn.zlp.user.controller;

import cn.zlp.basic.exception.BusinessException;
import cn.zlp.basic.util.AjaxResult;
import cn.zlp.user.dto.BinderDto;
import cn.zlp.user.dto.LoginDto;
import cn.zlp.user.dto.UserDto;
import cn.zlp.user.service.ILogininfoService;
import cn.zlp.user.service.IUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: pethome
 * @author: Mr.Zhang
 * @create: 2021-12-09 16:53
 **/
@RestController
@RequestMapping("/login")
@Api(tags = "登录接口接口",description = "登录接口详细描述")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 账号登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            AjaxResult ajaxResult = logininfoService.accountLogin(loginDto);
            return ajaxResult;
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登录失败," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 手机验证码登录
     * @param loginDto
     * @return
     */
    @PostMapping("/verification")
    public AjaxResult verificationLogin(@RequestBody UserDto userDto){
        try {
            AjaxResult ajaxResult = logininfoService.verificationLogin(userDto);
            return ajaxResult;
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登录失败," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 微信登录
     * @param map
     * @return
     */
    @PostMapping("/wxUser")
    public AjaxResult wxUserLogin(@RequestBody Map<String,String> map){
        try {
            AjaxResult ajaxResult = logininfoService.wxUserLogin(map);
            return ajaxResult;
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登录失败," + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误," + e.getMessage());
        }
    }

    /**
     * 微信绑定
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binderLogin(@RequestBody BinderDto binderDto){
        try {
            AjaxResult ajaxResult = logininfoService.toBinder(binderDto);
            return ajaxResult;
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("绑定失败"+e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误"+e.getMessage());
        }
    }

    @GetMapping("/signOut")
    public boolean signOut(HttpServletRequest request){
        String token = request.getHeader("token");
        redisTemplate.opsForValue().set(token,null);
        return true;
    }

}
